<template>
  <div class="topbar">
    <!-- 版心 -->
    <div class="wrap">
      <div class="topbarwrap">
        <div class="left">欢迎来到叩丁狼积分商城</div>
        <ul class="right">
          <li>
            <img :src="userInfo.headImg" alt="" />用户名：{{
              userInfo.nickName
            }}
          </li>
          <li>我的积分：{{ userInfo.coin }}</li>
          <li>获取积分</li>
          <li>叩丁狼官网</li>
          <li class="cart-btn" v-if="isLogined">
            <img src="../assets/img/cart.png" alt="" />
            <span>购物车</span>
            <strong>{{ cartTotal }}</strong>
          </li>
          <li class="loginBtn" @click="changeIsShowLoginModal(true)" v-else>
            登录
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
import { mapActions, mapMutations, mapState } from "vuex";
import { WeChatAPI } from "@/requst/api";
export default {
  data() {
    return {};
  },
  computed: {
    ...mapState({
      isLogined: (state) => state.loginStatus.isLoginStatus,
      userInfo: (state) => state.userInfo.userInfo,
      cartTotal: (state) => state.userInfo.cartTotal,
    }),
  },
  methods: {
    ...mapMutations({
      changeIsShowLoginModal: "showModal/changeIsShowLoginModal",
      changeIsLoginStatus: "loginStatus/changeIsLoginStatus",
      initUserInfo: "userInfo/initUserInfo",
    }),
    ...mapActions({
      asyncchangeUserInfo: "userInfo/asyncchangeUserInfo",
    }),
  },
  // watch: {
  //   "$route.path": {
  //     handler(newval, oldval) {
  //       console.log("地址改变了....");
  //       let token = localStorage.getItem("x-auth-token");
  //       this.changeIsLoginStatus(Boolean(token));
  //     },
  //   },
  // },
  created() {
    // console.log("tapbar--执行了created");
    this.$router.onReady(async () => {
      let paramscode = this.$route.query.code;
      if (paramscode) {
        let res = await WeChatAPI({
          code: paramscode,
        });
        // console.log("xxxxx123", res);
        //接口结果有三种 0  400  407
        if (res.data.code == 0) {
          //1.提示扫码登录成功
          alert("扫码登录成功");
          //2.保存token
          localStorage.setItem("x-auth-token", res.data["x-auth-token"]);
          //3.更新登录状态
          this.changeIsLoginStatus(true);
          // 4.清空地址栏中的code
          this.$router.push(this.$route.path);
          // 调用actions内方法请求用户信息
          this.asyncchangeUserInfo();
        } else if (res.data.code == 400) {
          // 二维码失效
          // 1.提示重新扫码
          alert("请重新扫码");
          // 2.弹出登录框
          this.changeIsShowLoginModal(true);
        } else if (res.data.code == 407) {
          // 第一次登录
          // 1.提示一次登录，需要再进行登录
          alert("请使用手机号登录一次");
          // 2.弹出登录框
          this.changeIsShowLoginModal(true);
          // 3.本地存储uuid （用户手机号登录需要用uuid来关联微信）
          localStorage.setItem("uuid", res.data.uuid);
          // 4.清除下地址栏中的code
          this.$router.push(this.$route.path);
        }
      } else {
        let token = localStorage.getItem("x-auth-token");
        this.changeIsLoginStatus(Boolean(token));
        if (token) {
          // 重新请求用户信息
          this.asyncchangeUserInfo();
        } else {
          // 否则初始化的数据
          this.initUserInfo();
        }
      }
    });
  },
};
</script>

<style lang = "less" scoped>
.topbar {
  height: 40px;
  line-height: 40px;
  background: #242b39;
  font-size: 14px;
  font-family: SourceHanSansSC;
  font-weight: 300;
  color: #fffefe;
  .topbarwrap {
    display: flex;
    justify-content: space-between;
    .right {
      display: flex;
      li {
        cursor: pointer;
        padding: 0 10px;
        text-align: center;
        img {
          width: 26px;
          height: 26px;
          vertical-align: middle;
          border-radius: 50%;
        }
        &.loginBtn {
          width: 124px;
          height: 40px;
          background: #0a328e;
        }
        &.cart-btn {
          width: 124px;
          height: 40px;
          background: #0a328e;
          text-align: center;
          img {
            width: 20px;
            height: 19px;
            vertical-align: middle;
          }
          span {
            margin: 0 8px;
          }
          strong {
            width: 22px;
            height: 22px;
            line-height: 22px;
            display: inline-block;
            border-radius: 50%;
            background-color: #fd604d;
          }
        }
      }
    }
  }
}
</style>